package com.wanho.qz103.hrsaas.sys.contoller;

import com.wanho.qz103.hrsaas.commons.annontations.*;
import com.wanho.qz103.hrsaas.commons.exception.ControllerException;
import com.wanho.qz103.hrsaas.commons.exception.ServiceException;
import com.wanho.qz103.hrsaas.commons.vo.R;
import com.wanho.qz103.hrsaas.sys.pojos.Department;
import com.wanho.qz103.hrsaas.sys.service.DepartmentService;
import com.wanho.qz103.hrsaas.sys.vo.DepartmentVO;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService ;

    @GetMapping("/company/{id}")
    public R rootCompany(HttpServletRequest request) throws ControllerException{
        try {
            String requestURI = request.getRequestURI();
            String[] split = requestURI.split("/");
            String id = split[split.length-1] ;
            // 此处：不创建表了 就直接怼死啦 此处部门表字段 与 组织架构字段不一致  也不创建pojos
            //Department department = departmentService.findById(id);
            Map<String,String> dataMap = new HashMap<>();
            dataMap.put("name","三国") ;
            dataMap.put("companyAddress","江苏南京秦淮普天路1号") ;
            dataMap.put("mailbox","zhangsan@126.com") ;
            dataMap.put("remarks","中国科技有限公司") ;
            return R.success("获得部门详情成功！",dataMap) ;
        } catch (RuntimeException e) {
            e.printStackTrace();
            return R.fail("获得部门详情失败！") ;
        }
    }


    @DeleteMapping("/company/department/{id}")
    public R remove(HttpServletRequest request) throws ControllerException{
        try {
            String requestURI = request.getRequestURI();
            String[] split = requestURI.split("/");
            String id = split[split.length-1] ;
            boolean removeRs = departmentService.remove(id);
            if (removeRs) {
                return R.success("删除部门成功！") ;
            }
            return R.fail("删除部门失败！") ;
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail("获得部门详情失败！") ;
        }
    }

    @PutMapping("/company/department/{id}")
    public R modify(HttpServletRequest request) throws ControllerException{
        try {
            // 获得请求参数
            Department department = new Department();
            BeanUtils.populate(department,request.getParameterMap());
            //调用业务层
            boolean modifyRs = departmentService.modify(department);
            if (modifyRs) {
                return R.success("修改部门成功！") ;
            }
            return R.success("修改部门失败！") ;
        } catch (IllegalAccessException | InvocationTargetException | ServiceException e) {
            e.printStackTrace();
            return R.fail("修改部门异常！") ;
        }
    }

    @GetMapping("/company/department/{id}")
    public R view(HttpServletRequest request) throws ControllerException{
        try {
            String requestURI = request.getRequestURI();
            String[] split = requestURI.split("/");
            String id = split[split.length-1] ;
            Department department = departmentService.findById(id);
            return R.success("获得部门详情成功！",department) ;
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail("获得部门详情失败！") ;
        }
    }

    @GetMapping("/company/department")
    public R list(HttpServletRequest request) throws ControllerException{
        try {
            DepartmentVO departmentVO = departmentService.list();
            return R.success("获得部门列表成功！",departmentVO) ;
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail("获得部门列表失败！") ;
        }
    }

    @PostMapping("/company/department")
    public R save(HttpServletRequest request) throws ControllerException{
        try {
            // 获得请求参数
            Department department = new Department();
            BeanUtils.populate(department,request.getParameterMap());
            //调用业务层
            boolean saveRs = departmentService.save(department);
            if (saveRs) {
                return R.success("新增部门成功！") ;
            }
            return R.success("新增部门失败！") ;
        } catch (IllegalAccessException | InvocationTargetException | ServiceException e) {
            e.printStackTrace();
            return R.fail("新增部门异常！") ;
        }
    }
}
